package com.medata.controller;

import com.medata.dataObject.*;
import com.medata.exeptions.YunExceptions;
import com.medata.service.*;
import com.medata.utils.TimeUtil;
import com.medata.vo.AccountVo;
import com.medata.vo.LockIpVo;
import com.medata.vo.logsVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.engine.spi.SessionImplementor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author ：zb
 * @date ：Created in 2019/5/24 0024 13:29
 * @description：
 */
@Controller
@Api
@Slf4j
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private DatabaselistService databaselistService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private AreafilterService areafilterService;
    @Autowired
    private LockipService lockipService;
    @Autowired
    private BlackListForIpService blackListForIpService;
    @Autowired
    private LogMakeDocumentService logMakeDocumentService;
    @Autowired
    private ActiondefService actiondefService;
    @Autowired
    private EntityManager entityManager;
    @Autowired
    private LogSearchService logSearchService;
    @Autowired
    private LogdetailService logdetailService;
    @Autowired
    private LogloginService logloginService;
    @Autowired
    private LogdispprjService logdispprjService;
    @Autowired
    private OailockipService oailockipService;
    @Autowired
    private GlobalsettingService globalsettingService;

    @RequestMapping(value = "/usersget/{page}/{pagesize}", method = RequestMethod.GET)
    public String usersget(Model model, @PathVariable Integer page, @PathVariable Integer pagesize) {
        List<Account> all = (accountService.findAllByPage(new PageRequest(page - 1, pagesize))).getContent();
        long count = accountService.findCount();
        int i = (int) count / pagesize;
        int pageCount = ((int) count % pagesize) == 0 ? i : i + 1;
        int diffValue = pageCount - page;
        model.addAttribute("pageCount", pageCount);
        model.addAttribute("accounts", all);
        model.addAttribute("page", page);
        model.addAttribute("pageSize", pagesize);
        model.addAttribute("prePage", page - 1);
        model.addAttribute("diffValue", diffValue);
        return "admin/adminManager";
    }

    @RequestMapping(value = "/userAdd", method = RequestMethod.GET)
    public String userAdd(Model model) {
        List<Databaselist> all = databaselistService.findAll();
        Map<String, String> cates = new HashMap<>();
        for (Databaselist databaselist : all) {
            cates.put(databaselist.getDbtitle(), null);
        }
        model.addAttribute("categorys", cates);
        return "admin/userUpdate";
    }

    @RequestMapping(value = "/userDelete", method = RequestMethod.POST)
    @ResponseBody
    public void userDelete(String str) {
        String[] names = str.split("@");
        for (String name : names) {
            if (name != null && !name.isEmpty()) {
                Account ac = accountService.findByUserName(name);
                if (ac != null) {
                    accountService.delete(ac);
                } else {
                    throw new YunExceptions(0, "沒有找到需要刪除的用戶");
                }
            }
        }
        return;
    }

    @RequestMapping(value = "/userUpdate/{str}", method = RequestMethod.GET)
    public String userUpdate(@PathVariable String str, Model model) {
        String[] names = str.split("@");
        String name = null;
        for (String s : names) {
            if (s != null && !s.isEmpty()) {
                name = s;
                List<Databaselist> all = databaselistService.findAll();
                Map<String, String> cates = new HashMap<>();
                for (Databaselist databaselist : all) {
                    cates.put(databaselist.getDbtitle(), null);
                }
                Account account = accountService.findByUserName(name);
                String dbids = account.getDbids();
                if (dbids != null && !dbids.isEmpty()) {
                    String[] ids = dbids.split(",");
                    for (String id : ids) {
                        if (id != null && !id.isEmpty()) {
                            Databaselist databaselist = databaselistService.findByDbid(Integer.valueOf(id));
                            cates.put(databaselist.getDbtitle(), "1");
                        }
                    }
                }
                model.addAttribute("categorys", cates);
                model.addAttribute("account", account);
                return "admin/userUpdate";
            }
        }
        return "error/404";
    }

    @RequestMapping(value = "/userPost", method = RequestMethod.POST)
    public String userAdd(AccountVo accountVo, Model model, HttpServletResponse response) {
        if (accountVo.getUserId() == null) {
            Account byUserName = accountService.findByUserName(accountVo.getUserName());
            if (byUserName != null) {
                try {
                    PrintWriter writer = response.getWriter();
                    writer.print("<script type='text/javascript'>alert('该用户名已经存在');history.go(-1)</script>");
                    writer.flush();
                    writer.close();
                    return null;
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new YunExceptions(0, "添加用户时,出现重复用户名");
                }
            }
        }

        if ((!accountVo.getRoleName().equalsIgnoreCase("编辑人员") && accountVo.getDcs() != "") && (!accountVo.getRoleName().equalsIgnoreCase("普通用户") && accountVo.getDcs() != "")) {
            try {
                PrintWriter writer = response.getWriter();
                writer.print("<script type='text/javascript'>alert('只能为编辑人员和普通用户配置权限');history.go(-1)</script>");
                writer.flush();
                writer.close();
                return null;
            } catch (Exception e) {
                throw new YunExceptions(0, "添加用户的时候出现bug");
            }
        }
        if ((accountVo.getRoleName().equalsIgnoreCase("编辑人员"))){
            if (accountVo.getDcs().isEmpty()){
                try {
                    PrintWriter writer = response.getWriter();
                    writer.print("<script type='text/javascript'>alert('必须为编辑人员设置权限');history.go(-1)</script>");
                    writer.flush();
                    writer.close();
                    return null;
                } catch (Exception e) {
                    throw new YunExceptions(0, "添加用户的时候出现bug");
                }
            }
        }
        Account account = new Account();
        BeanUtils.copyProperties(accountVo, account);
        Integer roleId = 0;
        switch (accountVo.getRoleName()) {
            case ("管理员"):
                roleId = 1;
                break;
            case ("编辑人员"):
                roleId = 2;
                break;
            case ("普通用户"):
                roleId = 3;
                break;
            case ("审核人员"):
                roleId = 4;
                break;
        }
        account.setRoleId(roleId);
        account.setAddDateTime(Timestamp.valueOf(TimeUtil.getCurDate()));
        String dcs = accountVo.getDcs();
        StringBuffer dbids = new StringBuffer("");
        if (dcs != null && !dcs.isEmpty()) {
            String[] dcList = dcs.split(",");
            for (int i = 0; i < dcList.length; i++) {
                Databaselist databaselist = databaselistService.findByDbtitle(dcList[i]);
                if (i != 0) {
                    dbids.append("," + databaselist.getDbid());
                } else {
                    dbids.append(databaselist.getDbid());
                }
            }
        }
        account.setDbids(dbids.toString());
        accountService.save(account);
        try {
            PrintWriter writer = response.getWriter();
            writer.print("<script type='text/javascript'>alert('保存成功');history.go(-1)</script>");
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new YunExceptions(0, "添加or修改用户的时候出错");
        }
        return null;
    }

    @RequestMapping(value = "/areaFilter/{page}", method = RequestMethod.GET)
    public ModelAndView areafilterGet(@PathVariable Integer page) {
        ModelAndView mv = new ModelAndView();
        Sort sort = new Sort(Sort.Direction.DESC, "areaid");
        Pageable pageable = new PageRequest(page - 1, 10, sort);
        Page<Areafilter> all = areafilterService.findAll(pageable);
        int totalPages = all.getTotalPages();
        long total = all.getTotalElements();
        List<Areafilter> areafilters = all.getContent();
        mv.addObject("areafilters", areafilters);
        mv.addObject("page", page);
        mv.addObject("prePage", page - 1);
        mv.addObject("pageCount", totalPages);
        mv.addObject("count", total);
        mv.addObject("pageSize", 10);
        mv.setViewName("admin/areaFilter");
        return mv;
    }

    @RequestMapping(value = "/areaFilterDelete", method = RequestMethod.POST)
    @ResponseBody
    public void areafilterDelete(String str) {
        String[] names = str.split("@");
        for (String name : names) {
            if (name != null && !name.isEmpty()) {
                Areafilter areafilter = areafilterService.findByAreaname(name);
                if (areafilter != null) {
                    lockipService.deleteByAreaid(areafilter.getAreaid());
                    areafilterService.delete(areafilter);
                } else {
                    throw new YunExceptions(0, "沒有找到需要刪除的用戶");
                }
            }
        }
    }


    @RequestMapping(value = "/areaFilterUpdate/{areaname}", method = RequestMethod.GET)
    public ModelAndView areaFilterUpdate(@PathVariable String areaname) {
        Areafilter areafilter = areafilterService.findByAreaname(areaname);
        ModelAndView mv = new ModelAndView();
        mv.addObject("areafilter", areafilter);
        mv.setViewName("admin/areaFilterForm");
        return mv;
    }

    @RequestMapping(value = "/areaFilterForm", method = RequestMethod.GET)
    public String areaFilterform() {
        return "admin/areaFilterForm";
    }

    @RequestMapping(value = "/areaFilterPost", method = RequestMethod.POST)
    @ResponseBody
    public void areaFilterAdd(Areafilter areafilter, HttpServletResponse response) {
        if (areafilter.getAreaid() == null) {
            Areafilter area = areafilterService.findByAreaname(areafilter.getAreaname());
            if (area != null) {
                try {
                    response.setHeader("Content-type", "text/html;charset=UTF-8");
                    PrintWriter writer = response.getWriter();
                    writer.print("<script type='text/javascript'>alert('已经存在');history.go(-1)</script>");
                    writer.flush();
                    writer.close();
                    return;
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new YunExceptions(0, "ip区域验重失败");
                }
            }
        }
        areafilterService.saveOrupdate(areafilter);
        try {
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            writer.print("<script type='text/javascript'>alert('保存成功');history.go(-1)</script>");
            writer.flush();
            writer.close();
            return;
        } catch (Exception e) {
            e.printStackTrace();
            throw new YunExceptions(0, "保存区域失败");
        }
    }

    @RequestMapping(value = "/LockIPsget/{page}", method = RequestMethod.GET)
    public ModelAndView LockIPsget(@PathVariable Integer page) {
        ModelAndView mv = new ModelAndView();
        Sort sort = new Sort(Sort.Direction.DESC, "ipid");
        Pageable pageable = new PageRequest(page - 1, 10, sort);
        Page<LockIp> all = lockipService.findall(pageable);
        List<LockIp> lockIps = all.getContent();
        int totalPages = all.getTotalPages();
        long total = all.getTotalElements();
        List<LockIpVo> lockIpVos = new ArrayList<>();
        for (LockIp lockIp : lockIps) {
            LockIpVo vo = new LockIpVo();
            BeanUtils.copyProperties(lockIp, vo);
            vo.setAreaname((areafilterService.findByAreaid(lockIp.getAreaid())).getAreaname());
            lockIpVos.add(vo);
        }
        mv.addObject("lockIps", lockIpVos);
        mv.addObject("page", page);
        mv.addObject("prePage", page - 1);
        mv.addObject("pageCount", totalPages);
        mv.addObject("count", total);
        mv.addObject("pageSize", 10);
        mv.setViewName("admin/LockIPManager");
        return mv;
    }

    @RequestMapping(value = "/LockIPsDelete", method = RequestMethod.POST)
    @ResponseBody
    public void lockpisDelete(String str) {
        String[] names = str.split("@");
        for (String name : names) {
            if (name != null && !name.isEmpty()) {
                lockipService.deleteByIpid(Integer.valueOf(name));
            } else {
                throw new YunExceptions(0, "沒有找到需要刪除的用戶");
            }
        }
    }

    @RequestMapping(value = "/LockIPupdate/{id}", method = RequestMethod.GET)
    public ModelAndView lockipupdate(@PathVariable Integer id) {
        List<Databaselist> databaselists = databaselistService.findAll();
        ModelAndView mv = new ModelAndView();
        List<Areafilter> all = areafilterService.findAll();
        List<String> areas = new ArrayList<>();
        if (all != null && all.size() > 0) {
            for (Areafilter areafilter : all) {
                areas.add(areafilter.getAreaname());
            }
        }
        if (id != 0) {
            LockIp lockIp = lockipService.findByIpid(id);
            LockIpVo lockIpVo = new LockIpVo();
            BeanUtils.copyProperties(lockIp, lockIpVo);
            Areafilter areafilter = areafilterService.findByAreaid(lockIp.getAreaid());
            lockIpVo.setAreaname(areafilter.getAreaname());
            String dbnames="";
            String[] dbids = lockIpVo.getDbid().split(",");
            for (String did:dbids){
                if (!did.isEmpty()){
                    Databaselist databaselist = databaselistService.findByDbid(Integer.valueOf(did));
                    dbnames=dbnames+","+databaselist.getDbname();
                }
            }
            lockIpVo.setDbname(dbnames);
            mv.addObject("lockIpVo", lockIpVo);
        }
        mv.addObject("areas", areas);
        mv.addObject("categorys", databaselists);
        mv.setViewName("admin/lockIP");
        return mv;
    }

    @RequestMapping(value = "/LockIpPost", method = RequestMethod.POST)
    @ResponseBody
    public void lockipPost(LockIpVo lockIpVo, HttpServletResponse response) {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        if (lockIpVo.getDbname() == null || lockIpVo.getDbname().equals("") || lockIpVo.getAreaname().equals("")) {
            try {
                PrintWriter writer = response.getWriter();
                writer.print("<script type='text/javascript'>alert('权限范围和区域为必填项');history.go(-1)</script>");
                writer.flush();
                writer.close();
                return;
            } catch (Exception e) {
                e.printStackTrace();
                throw new YunExceptions(0, "ip限制输入的时候没有输入权限范围");
            }
        }
        if (lockIpVo.getIpid() == null) {
            LockIp lockIp = lockipService.findByIpid(lockIpVo.getIpid());
            if (lockIp != null) {
                try {
                    PrintWriter writer = response.getWriter();
                    writer.print("<script type='text/javascript'>alert('已经存在');history.go(-1)</script>");
                    writer.flush();
                    writer.close();
                    return;
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new YunExceptions(0, "ip限制验重失败");
                }
            }
        }
        LockIp lockIp = new LockIp();
        BeanUtils.copyProperties(lockIpVo, lockIp);
        String[] dbnames = lockIpVo.getDbname().split(",");
        String dbids="";
        for (String name:dbnames){
            if (!name.isEmpty()){
                if (dbids.isEmpty()){
                    dbids=dbids+databaselistService.findByDbtitle(name).getDbid();
                }else{
                    dbids=dbids+","+databaselistService.findByDbtitle(name).getDbid();
                }
            }
        }
        lockIp.setDbid(dbids);
        lockIp.setAreaid((areafilterService.findByAreaname(lockIpVo.getAreaname())).getAreaid());
        lockIp.setIpBegin(0);
        lockIp.setIpEnd(0);
        lockipService.saveOrUpdate(lockIp);
        try {
            PrintWriter writer = response.getWriter();
            writer.print("<script type='text/javascript'>alert('保存成功');history.go(-1)</script>");
            writer.flush();
            writer.close();
            return;
        } catch (Exception e) {
            e.printStackTrace();
            throw new YunExceptions(0, "保存ip限制失败");
        }

    }

    @RequestMapping(value = "/blackIPget/{page}", method = RequestMethod.GET)
    public ModelAndView blackipGET(@PathVariable Integer page) {
        ModelAndView mv = new ModelAndView();
        Sort sort = new Sort(Sort.Direction.DESC, "blackTime");
        Pageable pageable = new PageRequest(page - 1, 10, sort);
        Page<BlackListForIp> all = blackListForIpService.findAll(pageable);
        List<BlackListForIp> blackips = all.getContent();
        long total = all.getTotalElements();
        int totalPages = all.getTotalPages();

        mv.addObject("blackIps", blackips);
        mv.addObject("page", page);
        mv.addObject("prePage", page - 1);
        mv.addObject("pageCount", totalPages);
        mv.addObject("count", total);
        mv.addObject("pageSize", 10);
        mv.setViewName("admin/blackIpManager");
        return mv;
    }

    @RequestMapping(value = "/blackipDel", method = RequestMethod.POST)
    @ResponseBody
    public void blackipDel(String str) {
        String[] names = str.split("@");
        for (String name : names) {
            if (name != null && !name.isEmpty()) {
                blackListForIpService.deleteByBlackIp(name);
            } else {
                throw new YunExceptions(0, "沒有找到需要刪除的用戶");
            }
        }
    }

    @RequestMapping(value = "/logmakeDocumentGet/{isDetail}/{startTime}/{endTime}/{actionName}/{dateType}/{page}", method = RequestMethod.GET)
    public ModelAndView logmakeDocumentGet(@PathVariable Integer isDetail, @PathVariable String startTime, @PathVariable String endTime, @PathVariable String actionName, @PathVariable Integer dateType, @PathVariable Integer page) {
        String t2=endTime;
        endTime=timeAdd(endTime);
        ModelAndView mv = new ModelAndView();
        //获取hibernate的connection
        SessionImplementor session = entityManager.unwrap(SessionImplementor.class);
        Connection connection = session.connection();
        long total = 0;
        String sql = null;
        Integer pageCount = 0;
        List<LogMakeDocument> logs = new ArrayList<>();
        List<logsVO> logsVOS = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime nowTime = LocalDateTime.now(ZoneOffset.of("+8"));
        String actionId = null;


        Actiondef actiondef = actiondefService.findByActionName(actionName);
        actionId = actiondef.getActionid();
        if (startTime.equals("1")) {
            LocalDateTime oldTime = nowTime.minusDays(8);
            startTime = oldTime.format(formatter);
            endTime = nowTime.format(formatter);
        }
        if (isDetail == 1) {
            Pageable pageable = new PageRequest(page - 1, 10);
            Page<LogMakeDocument> pages = logMakeDocumentService.findByLogtimeBetweenAndActionidOrderByLogtimeDesc(Timestamp.valueOf(startTime + " 00:00:00"), Timestamp.valueOf(endTime + " 00:00:00"), Integer.valueOf(actionId), pageable);
            pageCount = pages.getTotalPages();
            total = pages.getTotalElements();
            logs = pages.getContent();
            if (logs != null && logs.size() > 1) {
                for (LogMakeDocument logMakeDocument : logs) {
                    logsVO logsVO = new logsVO();
                    BeanUtils.copyProperties(logMakeDocument, logsVO);
                    logsVOS.add(logsVO);
                }
            }
        } else {
            if (dateType == 1) {
                sql = "select top 10 * from  (\n" +
                        "select k.UserName,datepart(dd,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                        "from LogMakeDocument k \n" +
                        "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(dd,k.LOGTIME),UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                        ") as h where ud not in (\n" +
                        "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                        "from LogMakeDocument k \n" +
                        "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(dd,k.LOGTIME),UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                        ") ORDER BY date desc";
            } else if (dateType == 2) {
                sql = "select top 10 * from  (\n" +
                        "select k.UserName,datepart(month ,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                        "from LogMakeDocument k \n" +
                        "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(month,k.LOGTIME),UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                        ") as h where ud not in (\n" +
                        "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                        "from LogMakeDocument k \n" +
                        "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(month,k.LOGTIME),UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                        ") ORDER BY date desc";
            } else {
                sql = "select top 10 * from  (\n" +
                        "select k.UserName,datepart(yy,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                        "from LogMakeDocument k \n" +
                        "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(yy,k.LOGTIME),UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                        ") as h where ud not in (\n" +
                        "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                        "from LogMakeDocument k \n" +
                        "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(yy,k.LOGTIME),UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                        ") ORDER BY date desc";
            }

            try {
                PreparedStatement ps = connection.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    logsVO logsVO = new logsVO();
                    logsVO.setUserName(rs.getString(1));
                    logsVO.setDate(rs.getString(2));
                    logsVO.setActionCounts(rs.getInt(3));
                    logsVOS.add(logsVO);
                    total++;
                }
                long t = total / 10;
                pageCount = (int) (t + (total % 10 == 0 ? 0 : 1));
            } catch (Exception e) {
                e.printStackTrace();
                throw new YunExceptions(0, "获取资源日志;统计日志出错");
            }
        }


        String[] actions = new String[]{
                "创建元数据", "创建对象", "删除元数据", "删除对象"
        };

        if (dateType == null) {
            dateType = 1;
        }

        mv.addObject("dateType", dateType);
        mv.addObject("logs", logsVOS);
        mv.addObject("actionName", actionName);
        mv.addObject("actions", actions);
        mv.addObject("startTime", startTime);
        mv.addObject("endTime", t2);
        mv.addObject("isDetail", isDetail);
        mv.addObject("page", Integer.valueOf(page));
        mv.addObject("diffValue", pageCount - page);
        mv.addObject("prePage", page - 1);
        mv.addObject("pageCount", pageCount);
        mv.addObject("count", total);
        mv.addObject("pageSize", 10);
        mv.setViewName("solrTable");


        mv.setViewName("admin/logmakeDocument");
        return mv;
    }

    @RequestMapping(value = "/logmakeDocumentDelete", method = RequestMethod.POST)
    @ResponseBody
    public void logmakeDocumentDelete(String startTime, String endTime, String actionName) {
        Actiondef actiondef = actiondefService.findByActionName(actionName);
        logMakeDocumentService.deleteByLogtimeBetweenAndActionid(Timestamp.valueOf(startTime + " 00:00:00"), Timestamp.valueOf(endTime + " 00:00:00"), Integer.valueOf(actiondef.getActionid()));
    }

    @RequestMapping(value = "/actiondefsGet", method = RequestMethod.GET)
    public ModelAndView actiondefGet() {
        ModelAndView mv = new ModelAndView();
        List<Actiondef> all = actiondefService.findAll();
        mv.addObject("actiondefs", all);
        mv.setViewName("admin/actiondefManager");
        return mv;
    }

    @RequestMapping(value = "/actiondefPost", method = RequestMethod.POST)
    @ResponseBody
    public void actiondefsPost(Actiondef actiondef) {
        Actiondef action = actiondefService.findByActionName(actiondef.getActionname());
        actiondef.setLastdeletetime(action.getLastdeletetime());
        actiondefService.saveOrUpdate(actiondef);
        System.out.println("ok");
    }

    @RequestMapping(value = "/logsearchGet/{isDetail}/{startTime}/{endTime}/{actionName}/{dateType}/{page}/{category}", method = RequestMethod.GET)
    public ModelAndView logsearchGet(@PathVariable Integer isDetail, @PathVariable String startTime, @PathVariable String endTime, @PathVariable String actionName, @PathVariable Integer dateType, @PathVariable Integer page, @PathVariable Integer category) {
        String t2=endTime;
        endTime=timeAdd(endTime);
        ModelAndView mv = new ModelAndView();
        SessionImplementor session = entityManager.unwrap(SessionImplementor.class);
        Connection connection = session.connection();
        long total = 0;
        String sql = null;
        Integer pageCount = 0;
        List<LogSearch> logSearches = new ArrayList<>();
        List<logsVO> logsVOS = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime nowTime = LocalDateTime.now(ZoneOffset.of("+8"));
        String actionId = null;

        Actiondef actiondef = actiondefService.findByActionName(actionName);
        actionId = actiondef.getActionid();
        if (startTime.equals("1")) {
            LocalDateTime oldTime = nowTime.minusDays(8);
            startTime = oldTime.format(formatter);
            endTime = nowTime.format(formatter);
        }
        if (isDetail == 1) {
            Sort sort = new Sort(Sort.Direction.DESC, "logTime");
            Pageable pageable = new PageRequest(page - 1, 10,sort);
            Page<LogSearch> pages = logSearchService.findByLogTimeBetweenAndActionId(Timestamp.valueOf(startTime + " 00:00:00"), Timestamp.valueOf(endTime + " 00:00:00"), Integer.valueOf(actionId), pageable);
            pageCount = pages.getTotalPages();
            total = pages.getTotalElements();
            logSearches = pages.getContent();
        } else {
            if (dateType == 1) {
                if (category == 1) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserName,datepart(dd,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(dd,k.LOGTIME),UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(dd,k.LOGTIME),UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else if (category == 2) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserIP,datepart(dd,k.LOGTIME) as date,count(*) as count ,UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(dd,k.LOGTIME),UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(dd,k.LOGTIME),UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else {
                    sql = "select top 10 * from  (\n" +
                            "select k.KeyWord,datepart(dd,k.LOGTIME) as date,count(*) as count ,KeyWord+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.KeyWord,datepart(dd,k.LOGTIME),KeyWord+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  KeyWord+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.KeyWord,datepart(dd,k.LOGTIME),KeyWord+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }
            } else if (dateType == 2) {
                if (category == 1) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserName,datepart(month,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(month,k.LOGTIME),UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(month,k.LOGTIME),UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else if (category == 2) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserIP,datepart(month,k.LOGTIME) as date,count(*) as count ,UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(month,k.LOGTIME),UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(month,k.LOGTIME),UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else {
                    sql = "select top 10 * from  (\n" +
                            "select k.KeyWord,datepart(month,k.LOGTIME) as date,count(*) as count ,KeyWord+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.KeyWord,datepart(month,k.LOGTIME),KeyWord+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  KeyWord+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.KeyWord,datepart(month,k.LOGTIME),KeyWord+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }
            } else {
                if (category == 1) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserName,datepart(yy,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(yy,k.LOGTIME),UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(yy,k.LOGTIME),UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else if (category == 2) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserIP,datepart(yy,k.LOGTIME) as date,count(*) as count ,UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(yy,k.LOGTIME),UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(yy,k.LOGTIME),UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else {
                    sql = "select top 10 * from  (\n" +
                            "select k.KeyWord,datepart(yy,k.LOGTIME) as date,count(*) as count ,KeyWord+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.KeyWord,datepart(yy,k.LOGTIME),KeyWord+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  KeyWord+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogSearch k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.KeyWord,datepart(yy,k.LOGTIME),KeyWord+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }
            }
            try {
                PreparedStatement ps = connection.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    logsVO logsVO = new logsVO();
                    logsVO.setActionCounts(rs.getInt(3));
                    logsVO.setDate(rs.getString(2));
                    logsVO.setUserName(rs.getString(1));
                    logsVOS.add(logsVO);
                    total++;
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new YunExceptions(0, "检索日志获取日志的时候出现bug");
            }

        }
        String[] actions = new String[]{
                "简单检索", "高级检索"
        };
        pageCount = total % 10 == 0 ? ((int) total / 10) : ((int) total / 10) + 1;
        mv.addObject("category", category);
        mv.addObject("logsVO", logsVOS);
        mv.addObject("dateType", dateType);
        mv.addObject("logs", logSearches);
        mv.addObject("actionName", actionName);
        mv.addObject("actions", actions);
        mv.addObject("startTime", startTime);
        mv.addObject("endTime", t2);
        mv.addObject("isDetail", isDetail);
        mv.addObject("page", Integer.valueOf(page));
        mv.addObject("diffValue", pageCount - page);
        mv.addObject("prePage", page - 1);
        mv.addObject("pageCount", pageCount);
        mv.addObject("count", total);
        mv.addObject("pageSize", 10);
        mv.setViewName("solrTable");
        mv.setViewName("admin/logsearchManager");
        return mv;
    }

    @RequestMapping(value = "/logdetailGet/{isDetail}/{startTime}/{endTime}/{category}/{dateType}/{page}", method = RequestMethod.GET)
    public ModelAndView logdetailGet(@PathVariable Integer isDetail, @PathVariable String startTime, @PathVariable String endTime, @PathVariable Integer dateType, @PathVariable Integer page, @PathVariable Integer category) {
        String t2=endTime;
        endTime=timeAdd(endTime);
        ModelAndView mv = new ModelAndView();
        SessionImplementor session = entityManager.unwrap(SessionImplementor.class);
        Connection connection = session.connection();
        long total = 0;
        String sql = null;
        Integer pageCount = 0;
        List<Logdetail> Logdetails = new ArrayList<>();
        List<logsVO> logsVOS = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime nowTime = LocalDateTime.now(ZoneOffset.of("+8"));
        String actionId = null;

        if (startTime.equals("1")) {
            LocalDateTime oldTime = nowTime.minusDays(8);
            startTime = oldTime.format(formatter);
            endTime = nowTime.format(formatter);
        }
        if (isDetail == 1) {
            Sort sort = new Sort(Sort.Direction.DESC, "logtime");
            Pageable pageable = new PageRequest(page - 1, 10,sort);
            Page<Logdetail> pages = logdetailService.findByLogtimeBetween(Timestamp.valueOf(startTime + " 00:00:00"), Timestamp.valueOf(endTime + " 00:00:00"), pageable);
            total = pages.getTotalElements();
            pageCount = pages.getTotalPages();
            Logdetails = pages.getContent();
        } else {
            if (dateType == 1) {
                if (category == 1) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserName,datepart(dd,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDetail k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "') group by k.UserName,datepart(dd,k.LOGTIME),UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDetail k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "') group by k.UserName,datepart(dd,k.LOGTIME),UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else if (category == 2) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserIP,datepart(dd,k.LOGTIME) as date,count(*) as count ,UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDetail k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "') group by k.UserIP,datepart(dd,k.LOGTIME),UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDetail k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "') group by k.UserIP,datepart(dd,k.LOGTIME),UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }
            } else if (dateType == 2) {
                if (category == 1) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserName,datepart(month,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDetail k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "') group by k.UserName,datepart(month,k.LOGTIME),UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDetail k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "') group by k.UserName,datepart(month,k.LOGTIME),UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else if (category == 2) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserIP,datepart(month,k.LOGTIME) as date,count(*) as count ,UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDetail k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "') group by k.UserIP,datepart(month,k.LOGTIME),UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDetail k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "') group by k.UserIP,datepart(month,k.LOGTIME),UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }
            } else {
                if (category == 1) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserName,datepart(yy,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDetail k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "') group by k.UserName,datepart(yy,k.LOGTIME),UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDetail k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "') group by k.UserName,datepart(yy,k.LOGTIME),UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else if (category == 2) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserIP,datepart(yy,k.LOGTIME) as date,count(*) as count ,UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDetail k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "') group by k.UserIP,datepart(yy,k.LOGTIME),UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDetail k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "') group by k.UserIP,datepart(yy,k.LOGTIME),UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }
            }
            try {
                PreparedStatement ps = connection.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    logsVO logsVO = new logsVO();
                    logsVO.setActionCounts(rs.getInt(3));
                    logsVO.setDate(rs.getString(2));
                    logsVO.setUserName(rs.getString(1));
                    logsVOS.add(logsVO);
                    total++;
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new YunExceptions(0, "检索日志获取日志的时候出现bug");
            }

        }
        String[] actions = new String[]{
                "简单检索", "高级检索"
        };
        pageCount = total % 10 == 0 ? ((int) total / 10) : ((int) total / 10) + 1;
        mv.addObject("category", category);
        mv.addObject("logsVO", logsVOS);
        mv.addObject("dateType", dateType);
        mv.addObject("logs", Logdetails);
        mv.addObject("actions", actions);
        mv.addObject("startTime", startTime);
        mv.addObject("endTime", t2);
        mv.addObject("isDetail", isDetail);
        mv.addObject("page", Integer.valueOf(page));
        mv.addObject("diffValue", pageCount - page);
        mv.addObject("prePage", page - 1);
        mv.addObject("pageCount", pageCount);
        mv.addObject("count", total);
        mv.addObject("pageSize", 10);
        mv.setViewName("solrTable");
        mv.setViewName("admin/logdetailManager");
        return mv;
    }

    @RequestMapping(value = "/logdetailDelete", method = RequestMethod.POST)
    @ResponseBody
    public void logmakeDocumentDelete(String startTime, String endTime) {
        logdetailService.deleteByLogtimeBetween(Timestamp.valueOf(startTime + " 00:00:00"),
                Timestamp.valueOf(endTime + " 00:00:00"));
    }

    @RequestMapping(value = "/logsearchDelete", method = RequestMethod.POST)
    @ResponseBody
    public void logsearchDelete(String startTime, String endTime, String actionName) {
        Actiondef actiondef = actiondefService.findByActionName(actionName);
        logSearchService.deleteByLogTimeBetweenAndActionId(
                Timestamp.valueOf(startTime+" 00:00:00"),Timestamp.valueOf(endTime+" 00:00:00"),Integer.valueOf(actiondef.getActionid())
        );

    }

    @RequestMapping(value = "/logloginGet/{isDetail}/{startTime}/{endTime}/{actionName}/{dateType}/{page}", method = RequestMethod.GET)
    public ModelAndView logloginGet(@PathVariable Integer isDetail, @PathVariable String startTime, @PathVariable String endTime, @PathVariable String actionName, @PathVariable Integer dateType, @PathVariable Integer page){
        String t2=endTime;
        endTime=timeAdd(endTime);
        ModelAndView mv = new ModelAndView();
        //获取hibernate的connection
        SessionImplementor session = entityManager.unwrap(SessionImplementor.class);
        Connection connection = session.connection();
        long total = 0;
        String sql = null;
        Integer pageCount = 0;
        List<Loglogin> logs = new ArrayList<>();
        List<logsVO> logsVOS = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime nowTime = LocalDateTime.now(ZoneOffset.of("+8"));
        String actionId = null;


        Actiondef actiondef = actiondefService.findByActionName(actionName);
        actionId = actiondef.getActionid();
        if (startTime.equals("1")) {
            LocalDateTime oldTime = nowTime.minusDays(8);
            startTime = oldTime.format(formatter);
            endTime = nowTime.format(formatter);
        }
        if (isDetail == 1) {
            Pageable pageable = new PageRequest(page - 1, 10);
            Page<Loglogin> pages = logloginService.findByLogtimeBetweenAndActionid(Timestamp.valueOf(startTime + " 00:00:00"), Timestamp.valueOf(endTime + " 00:00:00"), Integer.valueOf(actionId), pageable);
            pageCount = pages.getTotalPages();
            total = pages.getTotalElements();
            logs = pages.getContent();
        } else {
            if (dateType == 1) {
                if (actionId.equals("200")){
                    sql = "select top 10 * from  (\n" +
                            "select k.UserName,datepart(dd,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogLogin k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(dd,k.LOGTIME),UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogLogin k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(dd,k.LOGTIME),UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }else{
                    sql = "select top 10 * from  (\n" +
                            "select k.UserIP,datepart(dd,k.LOGTIME) as date,count(*) as count ,UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogLogin k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(dd,k.LOGTIME),UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogLogin k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(dd,k.LOGTIME),UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }
            } else if (dateType == 2) {
                if (actionId.equals("200")){
                    sql = "select top 10 * from  (\n" +
                            "select k.UserName,datepart(month ,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogLogin k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(month,k.LOGTIME),UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogLogin k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(month,k.LOGTIME),UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }else{
                    sql = "select top 10 * from  (\n" +
                            "select k.UserIP,datepart(month ,k.LOGTIME) as date,count(*) as count ,UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogLogin k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(month,k.LOGTIME),UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogLogin k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(month,k.LOGTIME),UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }
            } else {
                if (actionId.equals("200")){
                    sql = "select top 10 * from  (\n" +
                            "select k.UserName,datepart(yy,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogLogin k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(yy,k.LOGTIME),UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogLogin k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(yy,k.LOGTIME),UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }else{
                    sql = "select top 10 * from  (\n" +
                            "select k.UserIP,datepart(yy,k.LOGTIME) as date,count(*) as count ,UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogLogin k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(yy,k.LOGTIME),UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogLogin k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "'and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(yy,k.LOGTIME),UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }
            }

            try {
                PreparedStatement ps = connection.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    logsVO logsVO = new logsVO();
                    logsVO.setUserName(rs.getString(1));
                    logsVO.setDate(rs.getString(2));
                    logsVO.setActionCounts(rs.getInt(3));
                    logsVOS.add(logsVO);
                    total++;
                }
                long t = total / 10;
                pageCount = (int) (t + (total % 10 == 0 ? 0 : 1));
            } catch (Exception e) {
                e.printStackTrace();
                throw new YunExceptions(0, "获取资源日志;统计日志出错");
            }
        }


        String[] actions = new String[]{
                "创建元数据", "创建对象", "删除元数据", "删除对象"
        };

        if (dateType == null) {
            dateType = 1;
        }

        mv.addObject("dateType", dateType);
        mv.addObject("loglogins",logs);
        mv.addObject("logs", logsVOS);
        mv.addObject("actionName", actionName);
        mv.addObject("actions", actions);
        mv.addObject("startTime", startTime);
        mv.addObject("endTime", t2);
        mv.addObject("isDetail", isDetail);
        mv.addObject("page", Integer.valueOf(page));
        mv.addObject("diffValue", pageCount - page);
        mv.addObject("prePage", page - 1);
        mv.addObject("pageCount", pageCount);
        mv.addObject("count", total);
        mv.addObject("pageSize", 10);
        mv.setViewName("solrTable");


        mv.setViewName("admin/logloginManager");
        return mv;
    }

    @RequestMapping(value = "/logloginDelete",method = RequestMethod.POST)
    @ResponseBody
    public void logloginDelete(String startTime,String endTime,String actionName){
        Actiondef actiondef = actiondefService.findByActionName(actionName);
        logloginService.deleteByLogtimeBetweenAndActionid(Timestamp.valueOf(startTime+" 00:00:00"),
                Timestamp.valueOf(endTime+" 00:00:00"),Integer.valueOf(actiondef.getActionid()));
    }


    @RequestMapping(value = "/logdispprjGet/{isDetail}/{startTime}/{endTime}/{actionName}/{dateType}/{page}/{category}", method = RequestMethod.GET)
    public ModelAndView logdispprjGet(@PathVariable Integer isDetail, @PathVariable String startTime, @PathVariable String endTime, @PathVariable String actionName, @PathVariable Integer dateType, @PathVariable Integer page, @PathVariable Integer category){
        String t2=endTime;
        endTime=timeAdd(endTime);
        ModelAndView mv = new ModelAndView();
        SessionImplementor session = entityManager.unwrap(SessionImplementor.class);
        Connection connection = session.connection();
        long total = 0;
        String sql = null;
        Integer pageCount = 0;
        List<Logdispprj> logSearches = new ArrayList<>();
        List<logsVO> logsVOS = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime nowTime = LocalDateTime.now(ZoneOffset.of("+8"));
        String actionId = null;

        Actiondef actiondef = actiondefService.findByActionName(actionName);
        actionId = actiondef.getActionid();
        if (startTime.equals("1")) {
            LocalDateTime oldTime = nowTime.minusDays(8);
            startTime = oldTime.format(formatter);
            endTime = nowTime.format(formatter);
        }
        if (isDetail == 1) {
            Sort sort=new Sort(Sort.Direction.DESC,"logtime");
            Pageable pageable = new PageRequest(page - 1, 10,sort);
            Page<Logdispprj> pages = logdispprjService.findByLogtimeBetweenAndActionid(Timestamp.valueOf(startTime + " 00:00:00"), Timestamp.valueOf(endTime + " 00:00:00"), actionId, pageable);
            pageCount = pages.getTotalPages();
            total = pages.getTotalElements();
            logSearches = pages.getContent();
        } else {
            if (dateType == 1) {
                if (category == 1) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserName,datepart(dd,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(dd,k.LOGTIME),UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(dd,k.LOGTIME),UserName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else if (category == 2) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserIP,datepart(dd,k.LOGTIME) as date,count(*) as count ,UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(dd,k.LOGTIME),UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(dd,k.LOGTIME),UserIP+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else {
                    sql = "select top 10 * from  (\n" +
                            "select k.PrjectName,datepart(dd,k.LOGTIME) as date,count(*) as count ,PrjectName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.PrjectName,datepart(dd,k.LOGTIME),PrjectName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  PrjectName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.PrjectName,datepart(dd,k.LOGTIME),PrjectName+ CAST(datepart(dd,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }
            } else if (dateType == 2) {
                if (category == 1) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserName,datepart(month,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(month,k.LOGTIME),UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(month,k.LOGTIME),UserName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else if (category == 2) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserIP,datepart(month,k.LOGTIME) as date,count(*) as count ,UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(month,k.LOGTIME),UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(month,k.LOGTIME),UserIP+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else {
                    sql = "select top 10 * from  (\n" +
                            "select k.PrjectName,datepart(month,k.LOGTIME) as date,count(*) as count ,PrjectName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.PrjectName,datepart(month,k.LOGTIME),PrjectName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  PrjectName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.PrjectName,datepart(month,k.LOGTIME),PrjectName+ CAST(datepart(month,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }
            } else {
                if (category == 1) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserName,datepart(yy,k.LOGTIME) as date,count(*) as count ,UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(yy,k.LOGTIME),UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserName,datepart(yy,k.LOGTIME),UserName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else if (category == 2) {
                    sql = "select top 10 * from  (\n" +
                            "select k.UserIP,datepart(yy,k.LOGTIME) as date,count(*) as count ,UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(yy,k.LOGTIME),UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.UserIP,datepart(yy,k.LOGTIME),UserIP+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                } else {
                    sql = "select top 10 * from  (\n" +
                            "select k.PrjectName,datepart(yy,k.LOGTIME) as date,count(*) as count ,PrjectName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.PrjectName,datepart(yy,k.LOGTIME),PrjectName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") as h where ud not in (\n" +
                            "select top " + (page - 1) * 10 + "  PrjectName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8)) as ud\n" +
                            "from LogDispPrj k \n" +
                            "where (k.LOGTIME >='" + startTime + "' and k.LOGTIME<='" + endTime + "' and k.ACTIONID =" + actionId + ") group by k.PrjectName,datepart(yy,k.LOGTIME),PrjectName+ CAST(datepart(yy,k.LOGTIME) as VARCHAR(8))\n" +
                            ") ORDER BY date desc";
                }
            }
            try {
                PreparedStatement ps = connection.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    logsVO logsVO = new logsVO();
                    logsVO.setActionCounts(rs.getInt(3));
                    logsVO.setDate(rs.getString(2));
                    logsVO.setUserName(rs.getString(1));
                    logsVOS.add(logsVO);
                    total++;
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new YunExceptions(0, "检索日志获取日志的时候出现bug");
            }

        }
        String[] actions = new String[]{
                "用户访问日志", "IP访问"
        };
        pageCount = total % 10 == 0 ? ((int) total / 10) : ((int) total / 10) + 1;
        mv.addObject("category", category);
        mv.addObject("logsVO", logsVOS);
        mv.addObject("dateType", dateType);
        mv.addObject("logs", logSearches);
        mv.addObject("actionName", actionName);
        mv.addObject("actions", actions);
        mv.addObject("startTime", startTime);
        mv.addObject("endTime", t2);
        mv.addObject("isDetail", isDetail);
        mv.addObject("page", Integer.valueOf(page));
        mv.addObject("diffValue", pageCount - page);
        mv.addObject("prePage", page - 1);
        mv.addObject("pageCount", pageCount);
        mv.addObject("count", total);
        mv.addObject("pageSize", 10);
        mv.setViewName("solrTable");
        mv.setViewName("admin/logdispprjManager");
        return mv;
    }

    @RequestMapping(value = "/logdispprjDelete",method = RequestMethod.POST)
    @ResponseBody
    public void logdispprjDelete(String startTime, String endTime, String actionName){
        Actiondef actiondef = actiondefService.findByActionName(actionName);
        logdispprjService.deleteByLogtimeBetweenAndActionid(
                Timestamp.valueOf(startTime+" 00:00:00"),
                Timestamp.valueOf(endTime+" 00:00:00"),actiondef.getActionid()
        );
    }

    @RequestMapping(value = "/zlCount",method = RequestMethod.GET)
    public ModelAndView zlCount(){
        SessionImplementor session = entityManager.unwrap(SessionImplementor.class);
        Connection connection = session.connection();
        ModelAndView mv=new ModelAndView();
        List<Databaselist> all = databaselistService.findAll();
        List<List<String>> values=new ArrayList<>();
        for (Databaselist databaselist:all){
            String sql="select count(*) from "+databaselist.getDbname();
            String sql2="select count(*) from DocObject where dbid = "+databaselist.getDbid();
            try {
                PreparedStatement ps = connection.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                List<String> value=new ArrayList<>();
                while (rs.next()){
                    value.add(databaselist.getDbtitle());
                    value.add(rs.getString(1));
                }
                PreparedStatement ps2 = connection.prepareStatement(sql2);
                ResultSet rs2 = ps2.executeQuery();
                while(rs2.next()){
                    value.add(rs2.getString(1));
                }
                values.add(value);
            }catch (Exception e){
                e.printStackTrace();
                throw new YunExceptions(0,"获取资料库汇总的时候出现bug");
            }
        }
        mv.addObject("values",values);
        mv.setViewName("admin/zlCount");
        return mv;
    }

    @RequestMapping(value = "/zlVisit/{startTime}/{endTime}",method = RequestMethod.GET)
    public ModelAndView zlVisit(@PathVariable String startTime,@PathVariable String endTime){
        ModelAndView mv=new ModelAndView();
        SessionImplementor session = entityManager.unwrap(SessionImplementor.class);
        Connection connection = session.connection();
        List<Databaselist> all = databaselistService.findAll();
        List<List<String>> values=new ArrayList<>();
        for (Databaselist databaselist:all){
            String	sql="select count(*) from LogDetail where DocID in (select DocID from "+databaselist.getDbname()+" where dbid="+databaselist.getDbid()+") and (LOGTIME >= '" +startTime+" 00:00:00"+"') AND (LOGTIME <='" +endTime+" 00:00:00"+"')";
            String	sql2="select count(*) from LogDispPrj where prjectname in (select objectfilename from docobject where dbid="+databaselist.getDbid()+") and (LOGTIME >= '" +startTime+"') AND (LOGTIME <='" +endTime+"')";
            List<String> value=new ArrayList<>();
            try {
                PreparedStatement ps = connection.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()){
                    value.add(databaselist.getDbtitle());
                    value.add(rs.getString(1));
                }
                PreparedStatement ps2 = connection.prepareStatement(sql2);
                ResultSet rs2 = ps2.executeQuery();
                while (rs2.next()){
                    value.add(rs2.getString(1));
                }
                values.add(value);
            }catch (Exception e){
                e.printStackTrace();
                throw new YunExceptions(0,"获取访问总量出现bug");
            }
        }
        mv.addObject("startTime",startTime);
        mv.addObject("endTime",endTime);
        mv.addObject("values",values);
        mv.setViewName("admin/zlVisit");
        return mv;
    }

    @RequestMapping(value = "/oaManager/{page}",method = RequestMethod.GET)
    public ModelAndView oasGet(@PathVariable Integer page){
        ModelAndView mv=new ModelAndView();
        Pageable pageable=new PageRequest(page-1,10);
        Page<Oailockip> oas = oailockipService.findAll(pageable);
        Integer pageCount=oas.getTotalPages();
        long total=oas.getTotalElements();
        List<Oailockip> oalist = oas.getContent();
        mv.addObject("oas", oalist);
        mv.addObject("page", Integer.valueOf(page));
        mv.addObject("diffValue", pageCount - page);
        mv.addObject("prePage", page - 1);
        mv.addObject("pageCount", pageCount);
        mv.addObject("count", total);
        mv.addObject("pageSize", 10);
        mv.setViewName("solrTable");
        mv.setViewName("admin/oaManager");
        return mv;
    }

    @RequestMapping(value = "/oasDelete",method = RequestMethod.POST)
    @ResponseBody
    public void oasDelete(String str){
        String[] names = str.split("@");
        for (String name : names) {
            if (name != null && !name.isEmpty()) {
                oailockipService.deleteByIpid(Integer.valueOf(name));
            } else {
                throw new YunExceptions(0, "沒有找到需要刪除的用戶");
            }
        }
    }

    @RequestMapping(value = "/oaIp",method = RequestMethod.GET)
    public String oaip(){
        return "admin/oaIp";
    }

    @RequestMapping(value = "/oaPost",method = RequestMethod.POST)
    @ResponseBody
    public void  oapost(String ip1,String ip2,HttpServletResponse response){
        Oailockip oailockip=new Oailockip();
        oailockip.setIp1(ip1);
        oailockip.setIp2(ip2);
        oailockipService.saveOrUpdate(oailockip);
        try {
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            writer.print("<script type='text/javascript'>alert('保存成功');history.go(-2)</script>");
            writer.flush();
            writer.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @RequestMapping(value = "/sysSet",method = RequestMethod.GET)
    public ModelAndView sysSet(){
        ModelAndView mv=new ModelAndView();
        List<Globalsetting> all = globalsettingService.findAll();
        all.remove(34);
        all.remove(33);
        all.remove(32);
        all.remove(31);
        all.remove(30);
        all.remove(29);
        all.remove(28);
        mv.addObject("glo",all);
        mv.setViewName("admin/sysSet");
        return mv;
    }

    @RequestMapping(value = "/sysPost",method = RequestMethod.POST)
    @ResponseBody
    public String sysPOST(String name,String value){
        Globalsetting globalsetting = globalsettingService.findByGsetname(name);
        globalsetting.setGsetvalue(value);
        globalsettingService.save(globalsetting);
        return "success";
    }



    public static String timeAdd(String oldTime){
        oldTime=oldTime.trim()+" 12:24:12";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(oldTime, dtf);
        LocalDateTime dt2 = parse.plusDays(1);
        String s = dt2.format(dtf);
        return s.split(" ")[0];
    }

    public static void main(String[] args) {
        String str1="2018-07-05 12:24:12";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(str1, dtf);
        LocalDateTime parse2 = parse.plusDays(1);
        System.out.println(parse);
    }
}

